//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.web.controller.ky;

import com.aidong.dao.wills.CardTypeCodeDao;
import com.aidong.dao.wills.DicShopAreaCodeDao;
import com.aidong.dao.wills.KyBespeakDao;
import com.aidong.dao.wills.KyContractDao;
import com.aidong.dao.wills.KyTimeTableDao;
import com.aidong.dao.wills.MemberContractDao;
import com.aidong.dao.wills.MemberInfoDao;
import com.aidong.dao.wills.PtPriceDao;
import com.aidong.model.wills.CardTypeCode;
import com.aidong.model.wills.KyBespeak;
import com.aidong.model.wills.KyContract;
import com.aidong.model.wills.KyTimeTable;
import com.aidong.model.wills.MemberContract;
import com.aidong.model.wills.MemberInfo;
import com.aidong.model.wills.PtPrice;
import com.aidong.web.controller.BaseController;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"kyBespeak2"})
public class KyBespeak2Controller extends BaseController {
    @Resource
    KyTimeTableDao kyTimeTableDao;
    @Resource
    KyBespeakDao kyBespeakDao;
    @Resource
    KyContractDao kyContractDao;
    @Resource
    PtPriceDao ptPriceDao;
    @Resource
    protected MemberInfoDao memberInfoDao;
    @Resource
    MemberContractDao memberContractDao;
    @Resource
    CardTypeCodeDao cardTypeCodeDao;
    @Resource
    DicShopAreaCodeDao dicShopAreaCodeDao;

    public KyBespeak2Controller() {
    }

    @RequestMapping({"list"})
    public ModelAndView list() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return (new ModelAndView("ky/bespeak2/list")).addObject("dt", sdf.format(new Date()));
    }

    @RequestMapping({"_list"})
    public ModelAndView _list(@RequestParam(value = "date",required = false) String date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date dt1 = this.parseDate(date, sdf);
        if(dt1 == null) {
            dt1 = this.parseDate(sdf.format(new Date()), sdf);
        }

        Calendar cal = Calendar.getInstance();
        cal.setTime(dt1);
        cal.add(5, 8);
        Date dt2 = cal.getTime();
        SimpleDateFormat sdf1 = new SimpleDateFormat("M/d");
        ArrayList lsd = new ArrayList();
        cal.setTime(dt1);

        for(int hhmap = 0; hhmap < 8; ++hhmap) {
            lsd.add(sdf1.format(cal.getTime()));
            cal.add(5, 1);
        }

        LinkedHashMap var14 = new LinkedHashMap();

        for(int list = 8; list < 23; ++list) {
            var14.put(String.valueOf(list), new ArrayList());
        }

        List var15 = this.kyTimeTableDao.getListByShopDay(this.getLoginShopCode(), dt1, dt2);
        Iterator mv = var15.iterator();

        while(mv.hasNext()) {
            KyTimeTable t = (KyTimeTable)mv.next();
            cal.setTime(t.getClassTime());
            int h = cal.get(11);
            if(h < 8) {
                h = 8;
            }

            if(h > 22) {
                h = 22;
            }

            List ls = (List)var14.get(String.valueOf(h));
            if(ls != null) {
                ls.add(t);
            }
        }

        ModelAndView var16 = new ModelAndView("ky/bespeak2/_list");
        var16.addObject("hhmap", var14);
        var16.addObject("lsd", lsd);
        var16.addObject("dt", dt1);
        return var16;
    }

    @RequestMapping({"view"})
    public ModelAndView view(@RequestParam("id") int id) {
        KyTimeTable table = (KyTimeTable)this.kyTimeTableDao.getById(Integer.valueOf(id));
        if(table != null && table.getShopCode().equalsIgnoreCase(this.getLoginShopCode())) {
            List list = this.kyBespeakDao.getListByClass(id);
            ModelAndView mv = new ModelAndView("ky/bespeak2/view");
            mv.addObject("table", table);
            mv.addObject("list", list);
            if(table.getClassTime().getTime() > System.currentTimeMillis() + 60000L && table.getBespeakQuantity().intValue() < table.getPersonMax().intValue()) {
                mv.addObject("canBesp", Boolean.valueOf(true));
            }

            if(table.getClassTime().getTime() > System.currentTimeMillis() + 60000L) {
                mv.addObject("canCanc", Boolean.valueOf(true));
            }

            return mv;
        } else {
            return this.showAlert("没有这个课程");
        }
    }

    @RequestMapping({"add"})
    public ModelAndView add(@RequestParam("id") int id) {
        KyTimeTable table = (KyTimeTable)this.kyTimeTableDao.getById(Integer.valueOf(id));
        if(table != null && table.getShopCode().equalsIgnoreCase(this.getLoginShopCode())) {
            if(table.getClassTime().getTime() < System.currentTimeMillis()) {
                return this.showAlert("已经上课,不能预约");
            } else if(table.getBespeakQuantity().intValue() >= table.getPersonMax().intValue()) {
                return this.showAlert("人数已满");
            } else {
                ModelAndView mv = new ModelAndView("ky/bespeak2/add");
                mv.addObject("table", table);
                mv.addObject("_oper", "add");
                return mv;
            }
        } else {
            return this.showAlert("没有这个课程");
        }
    }

    @RequestMapping({"_add"})
    @ResponseBody
    public String _add(@ModelAttribute("table") KyBespeak bespeak) {
        if(this.chkNullZ(bespeak.getClassId())) {
            return this.getResult(false, "请选择课程");
        } else {
            KyTimeTable table = (KyTimeTable)this.kyTimeTableDao.getById(bespeak.getClassId());
            if(table != null && table.getShopCode().equalsIgnoreCase(this.getLoginShopCode())) {
                if(table.getClassTime().getTime() < System.currentTimeMillis()) {
                    return this.getResult(false, "已经上课,不能预约");
                } else if(table.getBespeakQuantity().intValue() >= table.getPersonMax().intValue()) {
                    return this.getResult(false, "人数已满");
                } else {
                    bespeak.setShopCode(this.getLoginShopCode());
                    bespeak.setBespeakDate(new Date());
                    bespeak.setClassType("2");
                    bespeak.setPtCode(table.getPtCode());
                    bespeak.setCoachCode(table.getCoachCode());
                    bespeak.setClassTime(table.getClassTime());
                    PtPrice price = this.ptPriceDao.getByCoachLevel(bespeak.getPtCode(), "01", 1);
                    if(price == null) {
                        return this.getResult(false, "不能购买");
                    } else if(this.chkNull(bespeak.getCardCode())) {
                        return this.getResult(false, "请输入K-Yoga卡号");
                    } else {
                        MemberInfo member = this.memberInfoDao.getByKyogaCard(bespeak.getCardCode());
                        if(member == null) {
                            return this.getResult(false, "K-Yoga卡号错误");
                        } else if(this.kyBespeakDao.chkMemberBespeak(bespeak.getClassId().intValue(), member.getMemberCode())) {
                            return this.getResult(false, "已经预约,不能重复预约");
                        } else {
                            KyContract contract = this.kyContractDao.getValidAmountByMemberCode(member.getMemberCode(), bespeak.getShopCode());
                            if(contract != null && contract.getEndTime() != null) {
                                if(contract.getEndTime().before(this.getTrimDate(new Date()))) {
                                    return this.getResult(false, "K-Yoga合同已过有效期");
                                } else if(contract.getFactAmount().doubleValue() + contract.getPresentAmount().doubleValue() - price.getPriceMin().doubleValue() < 0.0D) {
                                    return this.getResult(false, "会员剩余金额不足");
                                } else {
                                    bespeak.setCardCode(member.getKyogaCard());
                                    bespeak.setMemberCode(member.getMemberCode());
                                    bespeak.setCustName(member.getChName());
                                    bespeak.setMobile(member.getMobile());
                                    bespeak.setFactPrice(price.getPriceMin());
                                    bespeak.setStatus(Integer.valueOf(2));
                                    bespeak.setSalesCode(table.getCoachCode());
                                    bespeak.setCreateUser(this.getLoginUserCode());
                                    bespeak.setCreateTime(new Date());
                                    bespeak.setUpdateTime(new Date());

                                    try {
                                        this.kyBespeakDao.addBespeak(bespeak);
                                        this.addUserLog("预约K_Yoga团操", "编号:" + bespeak.getClassId() + ",课程:" + this.cache.getValue("ptItemName", bespeak.getPtCode()) + ",姓名:" + bespeak.getCustName());
                                    } catch (Exception var7) {
                                        this.log.error("add kybespeak", var7);
                                        return this.getResult(false, "发生错误");
                                    }

                                    return this.getResult(true, "预约成功");
                                }
                            } else {
                                return this.getResult(false, "会员没有有效的K-Yoga销售合同");
                            }
                        }
                    }
                }
            } else {
                return this.getResult(false, "没有这个课程");
            }
        }
    }

    @RequestMapping({"_cancel"})
    @ResponseBody
    public String _cancel(@RequestParam("id") int id) {
        KyBespeak bespeak = (KyBespeak)this.kyBespeakDao.getById(Integer.valueOf(id));
        if(bespeak != null && bespeak.getStatus().intValue() != 0) {
            if(bespeak.getStatus().intValue() == 1) {
                return this.getResult(false, "已经核销,不能取消");
            } else if(bespeak.getStatus().intValue() != 2) {
                return this.getResult(false, "不能取消");
            } else {
                bespeak.setStatus(Integer.valueOf(0));
                bespeak.setCanceType("1");
                bespeak.setCancelUser(this.getLoginUserCode());
                bespeak.setCreateTime(new Date());

                try {
                    this.kyBespeakDao.updateCancel(bespeak);
                    this.addUserLog("K_Yoga团操取消", "编号:" + bespeak.getAutoId() + ",课程:" + this.cache.getValue("ptItemName", bespeak.getPtCode()) + ",姓名:" + bespeak.getCustName());
                } catch (Exception var4) {
                    this.log.error("kybespeak cancel", var4);
                    return this.getResult(false, "取消发生错误");
                }

                return this.getResult(true, "取消成功");
            }
        } else {
            return this.getResult(false, "没有这个预约");
        }
    }

    @RequestMapping({"print"})
    public ModelAndView print(@RequestParam("id") int id) {
        KyTimeTable table = (KyTimeTable)this.kyTimeTableDao.getById(Integer.valueOf(id));
        if(table != null && table.getShopCode().equalsIgnoreCase(this.getLoginShopCode())) {
            List list = this.kyBespeakDao.getListByClass(id);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            StringBuilder sb = new StringBuilder();
            sb.append("{classId:").append(table.getClassId()).append("").append(",shopCode:\"").append(table.getShopCode()).append("\"").append(",shopName:\"").append(this.cache.getValue("shopName", table.getShopCode())).append("\"").append(",classTime:\"").append(sdf.format(table.getClassTime())).append("\"").append(",ptCode:\"").append(table.getPtCode()).append("\"").append(",ptCodeName:\"").append(this.cache.getValue("ptItemName", table.getPtCode())).append("\"").append(",coachCode:\"").append(table.getCoachCode()).append("\"").append(",coachName:\"").append(this.cache.getValue("ptCoachName", table.getCoachCode())).append("\"").append(",roomCode:\"").append(table.getRoomCode()).append("\"").append(",roomName:\"").append(this.cache.getValue("gcRoom", table.getShopCode() + "_" + table.getRoomCode())).append("\"").append(",personMax:\"").append(table.getPersonMax()).append("\"").append(",bespeakQuantity:\"").append(table.getBespeakQuantity()).append("\"").append(",clearQuantity:\"").append(table.getClearQuantity()).append("\"").append(",printDate:\"").append(sdf.format(new Date())).append("\"").append(",bespeak:[");
            int n = 0;

            KyBespeak bespeak;
            for(Iterator mv = list.iterator(); mv.hasNext(); sb.append("{saleId:").append(bespeak.getAutoId()).append("").append(",bespeakTime:\"").append(sdf.format(bespeak.getBespeakDate())).append("\"").append(",classTime:\"").append(sdf.format(bespeak.getClassTime())).append("\"").append(",clearTime:\"").append(bespeak.getClearTime() == null?"":sdf.format(bespeak.getClearTime())).append("\"").append(",statusName:\"").append(this.cache.getValue("bespeakStatus", String.valueOf(bespeak.getStatus()))).append("\"").append(",cardCode:\"").append(this.nvl(bespeak.getCardCode())).append("\"").append(",memberCode:\"").append(this.nvl(bespeak.getMemberCode())).append("\"").append(",memberName:\"").append(bespeak.getCustName()).append("\"").append(",mobile:\"").append(bespeak.getMobile()).append("\"").append("}")) {
                bespeak = (KyBespeak)mv.next();
                if(n++ > 0) {
                    sb.append(",");
                }
            }

            sb.append("]}");
            ModelAndView var9 = new ModelAndView("print/print");
            var9.addObject("data", sb.toString());
            var9.addObject("title", "团操上课信息");
            var9.addObject("ptjs", "gcclass");
            var9.addObject("_picPath", this.getSetting("pic.path"));
            return var9;
        } else {
            return this.showAlert("没有这个课程");
        }
    }

    @RequestMapping({"getMember"})
    @ResponseBody
    public String getMember(@RequestParam("cardCode") String cardCode) {
        MemberInfo member = this.memberInfoDao.getByKyogaCard(cardCode);
        if(member == null) {
            return this.getResult(false, "K-Yoga卡号错误");
        } else {
            KyContract contract = this.kyContractDao.getValidAmountByMemberCode(member.getMemberCode(), this.getLoginShopCode());
            if(contract != null && contract.getEndTime() != null) {
                if(contract.getEndTime().before(this.getTrimDate(new Date()))) {
                    return this.getResult(false, "K-Yoga合同已过有效期");
                } else {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    StringBuilder sb = new StringBuilder();
                    sb.append("{code:\"").append(member.getMemberCode()).append("\"").append(",card:\"").append(member.getKyogaCard()).append("\"").append(",shop:\"").append(member.getShopCode()).append("\"").append(",name:\"").append(member.getChName()).append("\"").append(",gender:\"").append(member.getGender()).append("\"").append(",genderName:\"").append(this.cache.getValue("gender", member.getGender())).append("\"").append(",idType:\"").append(member.getIdType()).append("\"").append(",idTypeName:\"").append(this.cache.getValue("idType", member.getIdType())).append("\"").append(",idNumber:\"").append(member.getIdNumber()).append("\"").append(",mobile:\"").append(member.getMobile()).append("\"").append(",memberPhoto:\"").append(this.nvl(member.getMemberPhoto())).append("\"").append(",idCardPhoto:\"").append(this.nvl(member.getIdCardPhoto())).append("\"").append(",memberCard:\"").append(this.nvl(member.getCardCode())).append("\"").append(",factAmount:\"").append(this.nvl(contract.getFactAmount())).append("\"").append(",presentAmount:\"").append(this.nvl(contract.getPresentAmount())).append("\"").append(",contractEnd:\"").append(contract.getEndTime() == null?"":sdf.format(contract.getEndTime())).append("\"").append("}");
                    return this.getSuccess(sb.toString(), "");
                }
            } else {
                return this.getResult(false, "会员没有有效的K-Yoga销售合同");
            }
        }
    }

    private boolean chkc(String memberCode) {
        List list = this.memberContractDao.getCurrentListByMemberCode(memberCode);
        if(list != null && list.size() != 0) {
            String shopCode = this.getLoginShopCode();
            Iterator i$ = list.iterator();

            while(i$.hasNext()) {
                MemberContract contract = (MemberContract)i$.next();
                if(shopCode.equalsIgnoreCase(contract.getShopCode())) {
                    return true;
                }

                if(this.memberContractDao.chkContractShop(contract.getContractCode(), shopCode)) {
                    return true;
                }

                if(!this.chkNull(contract.getManyShop())) {
                    CardTypeCode cardType = (CardTypeCode)this.cardTypeCodeDao.getById(contract.getCardType());
                    if(cardType != null && cardType.getManyShopQuantity().intValue() == 0 && this.dicShopAreaCodeDao.chkAreaShop(contract.getManyShop(), shopCode)) {
                        return true;
                    }
                }
            }

            return false;
        } else {
            return false;
        }
    }
}
